import xml.etree.cElementTree as ET
import MySQLdb
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

"""
table_type = "posthistory"
file_path = "D:\Research\ISEC-2014\data\content"
database = "isec2014"
user = "root"
password="123"
port=3306

"""
table_type = "posthistory"
file_path = "E:\Sangeeta\dataset\\2013_05_stack_overflow\Content"
database = "sangeeta"
user = "sangeetal"
password="sangeetal"
port=3307
#"""

db1= MySQLdb.connect(host="localhost", user=user, passwd=password,db=database, port=port)
insert_cursor = db1.cursor() 

file = open("site_names")
for line in file:
    site = line.strip() +".com"
    print  "site=",site
    table  = table_type+"_"+line.strip()+"_aug13"
    tree = ET.ElementTree(file=file_path+"\\"+site+"\\"+table_type+".xml")
    root= tree.getroot()
    #print root.tag, root.attrib
    count = 0
    for child_of_root in root:
        #print  child_of_root.attrib
        Comment = ""
        Text = ""
        try:
            Comment=(str)(child_of_root.attrib['Comment'])
            Comment = Comment.replace("\n"," ")
            Comment = Comment.replace("'"," ")
            Comment = Comment.replace("\""," ")
            Comment = Comment.replace(",", " ")
            Comment = Comment.replace("(", " ")
            Comment = Comment.replace(")", " ")
            Comment = Comment.replace("\\", " ")
        except Exception ,e:
            #print "error"            
            count = count+1
            #print e, "count1=", count
        
        
        try:
            Text=(str)(child_of_root.attrib['Text']).replace("\r"," ")
            Text = Text.replace("\n"," ")
            Text = Text.replace("'"," ")
            Text = Text.replace("\""," ")
            Text = Text.replace(",", " ")
            Text = Text.replace("("," ")
            Text = Text.replace(")", " ")
            Text = Text.replace("\\", " ")
        except Exception, e:
            #print "error"
            count = count+1
        try:
            UserId_or_name=(str)(child_of_root.attrib['UserId'])
        except Exception, e:
            count  =count+1 
            try:
                UserId_or_name=(str)(child_of_root.attrib['UserDisplayName'])
                UserId_or_name = UserId_or_name.replace("'"," ")
            except Exception, e:
                UserId="not given"
                count = count+1
            
        insert_str = "insert into `"+ table+ "` values("+child_of_root.attrib['Id']+","+child_of_root.attrib['PostHistoryTypeId']+","+\
        child_of_root.attrib['PostId']+",'"+child_of_root.attrib['RevisionGUID']+"','"+child_of_root.attrib['CreationDate']+"','"+\
        UserId_or_name+"','"+Comment+"','"+Text+"')"
         
        #print "insert = ", insert_str
        insert_cursor.execute(insert_str)
        #break
    db1.commit()
    print "Number of errors = ", count



#http://css.dzone.com/articles/processing-xml-python